// 控制loading状态的vuex
const loading = {
  state: {
    show: false, // 是否展示全局loading
    text: '', // loading 文字
    icon: 'loading', // loading图标
    style: {} // loading样式
  },

  mutations: {
    showLoading(state, payload = {}) {
      // #ifdef H5
      state.show = true;
      state.text = payload.text || '加载中';
      state.icon = payload.icon || 'loading';
      state.style = payload.style || {};
      // #endif
      // #ifndef H5
      uni.showLoading({
        title: payload.text || '加载中',
      });
      // #endif
    },

    hideLoading(state) {
      // #ifdef H5
      state.show = false;
      state.text = '';
      state.icon = 'loading';
      state.style = {};
      // #endif
      // #ifndef H5
      uni.hideLoading();
      // #endif
    },

    showSuccessTips(state, payload) {
      // #ifdef H5
      state.show = true;
      state.text = payload || '执行成功';
      state.icon = 'success';
      
      setTimeout(() => {
        state.show = false;
      }, 1500);
      // #endif
      // #ifndef H5
      uni.showToast({
        icon: "success",
        title: payload || '执行成功',
        duration: 1500
      });
      // #endif
    },

    showToast(state, payload) {
      if (payload) uni.showToast({ title: payload, icon: 'none' });
    }
  }
};

export default loading;
